package ${packageName};

import java.util.List;
import cn.hutool.core.util.StrUtil;
import com.it.cloud.core.Where;
import com.it.cloud.core.enums.MethodEnum;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.it.cloud.core.R;
import com.it.cloud.core.dao.BaseDao;
import com.it.cloud.core.dao.page.PageBean;
import lombok.AllArgsConstructor;

/**
* @author lt
*/
@RestController
@AllArgsConstructor
public class ${name}Controller implements ${name}Api {

	private BaseDao baseDao;
	private ${name}Service ${name?uncap_first}Service;

	public R<?> validate(${name} obj, MethodEnum method) {
		if (method.equals(MethodEnum.ADD)) {
			//新增验证逻辑
			return R.success();
		} else if (method.equals(MethodEnum.UPDATE)) {
			//更新验证逻辑
			return R.success();
		}
		return R.success();
	}

	@PostMapping("${mapping}/add")
	@Override
	public R<?> add(${name} obj) {
		R<?> validateResult = validate(obj, MethodEnum.ADD);
		if (validateResult.isError()) {
			return validateResult;
		}
		${name?uncap_first}Service.add(obj);
		return R.success();
	}
	
	@PostMapping("${mapping}/del")
	@Override
	public R<?> del(@RequestParam("ids") List<Long> idList) {
		${name?uncap_first}Service.del(idList);
		return R.success();
	}
	
	@PostMapping("${mapping}/update")
	@Override
	public R<?> update(${name} obj) {
		R<?> validateResult = validate(obj, MethodEnum.UPDATE);
		if (validateResult.isError()) {
			return validateResult;
		}
		${name?uncap_first}Service.update(obj);
		return R.success();
	}
	
	@PostMapping("${mapping}/batch")
	@Override
	public R<?> batch(
		@RequestParam("ids") List<Long> idList,
<#list batchProNames as name>
		Boolean ${name}Ck,
</#list>
		${name} obj
	) {
		${name?uncap_first}Service.batchUpdate(idList,<#list batchProNames as proName> ${proName}Ck,</#list> obj);
		return R.success();
	}
	
	@GetMapping("${mapping}/info")
	@Override
	public R<${name}> info(Long id) {
		${name} obj = baseDao.findById(${name}.class, id);
		return R.success(obj);
	}
	
	@GetMapping("${mapping}/list")
	@Override
	public R<List<${name}>> list() {
		List<${name}> list = baseDao.findAll(${name}.class, ${name}.ID);
		return R.success(list);
	}
	
	@GetMapping("${mapping}/page")
	@Override
	public R<PageBean<${name}>> page(Integer page, Integer pageSize, String searchStr) {
		Where where = Where.build();
		if (StrUtil.isNotBlank(searchStr)) {
			where.add(${name}.SEARCH_STR + " like ? ", "%" + searchStr + "%");
		}
		PageBean<${name}> pb = baseDao.page(${name}.class, where.condition(), where.params(), page, pageSize, ${name}.ID);
		return R.success(pb);
	}
}
